#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtDebug>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    sp = new QTextToSpeech(this);
    this->setWindowIcon(QIcon(":img/aaa.png"));
    this->setWindowTitle("桥洞盖小被的计算器");
    this->setStyleSheet("MainWindow{border-image: url(:/img/bbb.png);}");
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::Calculation()
{
    if (text.contains("+", Qt::CaseSensitive))
    {
        QStringList mylist = text.split("+");
        text = QString::number(mylist.at(0).toInt() + mylist.at(1).toInt());
        ui->lineEdit->setText(text);
    }
    else if (text.contains("-", Qt::CaseSensitive))
    {
        QStringList mylist = text.split("-");
        text = QString::number(mylist.at(0).toInt() - mylist.at(1).toInt());
        ui->lineEdit->setText(text);
    }
    else if (text.contains("*", Qt::CaseSensitive))
    {
        QStringList mylist = text.split("*");
        text = QString::number(mylist.at(0).toInt() * mylist.at(1).toInt());
        ui->lineEdit->setText(text);
    }
    else if (text.contains("/", Qt::CaseSensitive))
    {
        QStringList mylist = text.split("/");
        text = QString::number(mylist.at(0).toInt() / mylist.at(1).toInt());
        ui->lineEdit->setText(text);
    }

     sp->say("等于"+ text);
}

void MainWindow::on_pushButton_14_clicked()
{
    if (text.length() == 1 && text.at(0) == '0')
        return;

    ui->lineEdit->setText(text + "0");
    text = ui->lineEdit->text();
    sp->say("0");
}

void MainWindow::on_pushButton_clicked()
{
    if (ui->lineEdit->text() == "0")
    {
        ui->lineEdit->setText("1");
    }
    else
    {
        ui->lineEdit->setText(text + "1");
    }
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_2_clicked()
{
    if (ui->lineEdit->text() == "0")
    {
        ui->lineEdit->setText("2");
    }
    else
    {
        ui->lineEdit->setText(text + "2");
    }
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_5_clicked()
{
    if (ui->lineEdit->text() == "0")
    {
        ui->lineEdit->setText("4");
    }
    else
    {
        ui->lineEdit->setText(text + "4");
    }
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_6_clicked()
{
    if (ui->lineEdit->text() == "0")
    {
        ui->lineEdit->setText("5");
    }
    else
    {
        ui->lineEdit->setText(text + "5");
    }
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_7_clicked()
{
    if (ui->lineEdit->text() == "0")
    {
        ui->lineEdit->setText("6");
    }
    else
    {
        ui->lineEdit->setText(text + "6");
    }
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_9_clicked()
{
    if (ui->lineEdit->text() == "0")
    {
        ui->lineEdit->setText("7");
    }
    else
    {
        ui->lineEdit->setText(text + "7");
    }
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_10_clicked()
{
    if (ui->lineEdit->text() == "0")
    {
        ui->lineEdit->setText("8");
    }
    else
    {
        ui->lineEdit->setText(text + "8");
    }
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_11_clicked()
{
    if (ui->lineEdit->text() == "0")
    {
        ui->lineEdit->setText("9");
    }
    else
    {
        ui->lineEdit->setText(text + "9");
    }
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_4_clicked()
{
    if (text.right(1) == '+' || text.right(1) == '-' || text.right(1) == '*' || text.right(1) == '/')
    {
        text.chop(1);
        text.append("+");
        ui->lineEdit->setText(text);
        return;
    }

    if (text.contains("+", Qt::CaseSensitive) || text.contains("-", Qt::CaseSensitive) || text.contains("*", Qt::CaseSensitive) || text.contains("/", Qt::CaseSensitive))
    {
        Calculation();
        return;
    }

    if (text.isEmpty())
    {
        return;
    }

    ui->lineEdit->setText(text + "+");
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_8_clicked()
{
    if (text.right(1) == '+' || text.right(1) == '-' || text.right(1) == '*' || text.right(1) == '/')
    {
        text.chop(1);
        text.append("-");
        ui->lineEdit->setText(text);
        return;
    }

    if (text.contains("+", Qt::CaseSensitive) || text.contains("-", Qt::CaseSensitive) || text.contains("*", Qt::CaseSensitive) || text.contains("/", Qt::CaseSensitive))
    {
        Calculation();
        return;
    }

    if (text.isEmpty())
    {
        return;
    }

    ui->lineEdit->setText(text + "-");
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_12_clicked()
{
    if (text.right(1) == '+' || text.right(1) == '-' || text.right(1) == '*' || text.right(1) == '/')
    {
        text.chop(1);
        text.append("*");
        ui->lineEdit->setText(text);
        return;
    }

    if (text.contains("+", Qt::CaseSensitive) || text.contains("-", Qt::CaseSensitive) || text.contains("*", Qt::CaseSensitive) || text.contains("/", Qt::CaseSensitive))
    {
        Calculation();
        return;
    }

    if (text.isEmpty())
    {
        return;
    }

    ui->lineEdit->setText(text + "*");
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_15_clicked()
{
    if (text.right(1) == '+' || text.right(1) == '-' || text.right(1) == '*' || text.right(1) == '/')
    {
        text.chop(1);
        text.append("/");
        ui->lineEdit->setText(text);
        return;
    }

    if (text.contains("+", Qt::CaseSensitive) || text.contains("-", Qt::CaseSensitive) || text.contains("*", Qt::CaseSensitive) || text.contains("/", Qt::CaseSensitive))
    {
        Calculation();
        return;
    }

    if (text.isEmpty())
    {
        return;
    }

    ui->lineEdit->setText(text + "/");
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_13_clicked()
{
    if (text.contains("+", Qt::CaseSensitive) || text.contains("-", Qt::CaseSensitive) || text.contains("*", Qt::CaseSensitive) || text.contains("/", Qt::CaseSensitive))
    {
        Calculation();
        return;
    }
    if (text.isEmpty())
    {
        ui->lineEdit->clear();
        return;
    }
    text.chop(1); // 删除最后一个字符
    ui->lineEdit->setText(text);
}

void MainWindow::on_pushButton_3_clicked()
{
    ui->lineEdit->setText(text + "3");
    text = ui->lineEdit->text();
}

void MainWindow::on_pushButton_16_clicked()
{
    QString lastChar = text.right(1);
    if ((lastChar == "+" || lastChar == "-" || lastChar == "*" || lastChar == "/"))
        return;

    if (text.contains("+", Qt::CaseSensitive) || text.contains("-", Qt::CaseSensitive) || text.contains("*", Qt::CaseSensitive) || text.contains("/", Qt::CaseSensitive))
    {
        Calculation();
    }
}
